From df3a3e708653ca1cdb0eda77bbda5cb8de177571 Mon Sep 17 00:00:00 2001
From: "Van Bemten, Lionel (Nokia - BE/Antwerp)" <lionel.van_bemten@nokia.com>
Date: Tue, 10 Oct 2017 19:51:18 +0200
Subject: [PATCH] No runtime tests for type sizes

Replace build and execution of runtime test programs for determining
some type sizes of the target with compile time test programs.

This improves support for cross-compilation.

Signed-off-by: "Van Bemten, Lionel (Nokia - BE/Antwerp)" <lionel.van_bemten@nokia.com>
Signed-off-by: Eric Le Bihan <eric.le.bihan.dev@free.fr>
---
 configure | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 61 insertions(+), 16 deletions(-)

diff --git a/configure b/configure
index f2a77f3..8348b1f 100755
--- a/configure
+++ b/configure
@@ -154,25 +154,70 @@ choose () {
   fi
 }
 
+findtypesize () {
+  typ=$1
+  abbr=$2
+  r=false
+  type_size=0
+  while true; do
+    cat>trysizeof${abbr}.c<<EOF
+#include <sys/types.h>
+
+int main(void)
+{
+  static int v = 1 / !!((sizeof($typ) == $type_size));
+  return 0;
+}
+EOF
+    if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o trysizeof${abbr} trysizeof${abbr}.c 2>/dev/null; then
+        r=true
+        break
+    fi
+    type_size=$(expr $type_size + 1)
+    test $type_size -le 16 || break
+  done
+  rm -f trysizeof${abbr} trysizeof${abbr}.c
+  test $r = true || fail "$0: unable to determine size of $typ"
+  caps=$(echo "sizeof${abbr}" | tr a-z A-Z)
+  echo "#define ${package_macro_name}_${caps} $type_size" >> $sysdeps/sysdeps.h
+  echo "sizeof${abbr}: $type_size" >> $sysdeps/sysdeps
+}
+
+findtypesign () {
+  typ=$1
+  abbr=$2
+  caps=$(echo "signed${abbr}" | tr a-z A-Z)
+  cat>trysignof${abbr}.c<<EOF
+#include <sys/types.h>
+
+int main(void)
+{
+  static int v = 1 / !!(((($typ) -1) < 0));
+  return 0;
+}
+EOF
+  if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o trysignof${abbr} trysignof${abbr}.c 2>/dev/null; then
+    echo "#define ${package_macro_name}_HASSIGNED${caps}" >> $sysdeps/sysdeps.h
+    echo "#undef ${package_macro_name}_HASUNSIGNED${caps}" >> $sysdeps/sysdeps.h
+    echo "signed${abbr}: yes" >> $sysdeps/sysdeps
+  else
+    echo "#undef ${package_macro_name}_HASSIGNED${caps}" >> $sysdeps/sysdeps.h
+    echo "#define ${package_macro_name}_HASUNSIGNED${caps}" >> $sysdeps/sysdeps.h
+    echo "signed${abbr}: no" >> $sysdeps/sysdeps
+  fi
+  rm -f trysignof${abbr} trysignof${abbr}.c
+}
+
 trytypes () {
   echo "Checking size and signedness of standard types..."
-  $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o output-types src/sysdeps/output-types.c
-  ./output-types >> $sysdeps/sysdeps
-  ./output-types | grep -F sizeof | while read key value ; do
-    caps=$(echo $key | sed s/:\$// | tr a-z A-Z)
-    echo "#define ${package_macro_name}_${caps} $value" >> $sysdeps/sysdeps.h
+  for t in "unsigned short" "unsigned int" "unsigned long"; do
+    abbr=$(echo "$t" | sed -e 's/nsigned //')
+    findtypesize "$t" "${abbr}"
   done
-  ./output-types | grep -F signed | while read key value ; do
-    caps=$(echo $key | sed s/:\$// | tr a-z A-Z)
-    if test $value = yes ; then
-      echo "#define ${package_macro_name}_HASSIGNED${caps}"
-      echo "#undef ${package_macro_name}_HASUNSIGNED${caps}"
-    else
-      echo "#undef ${package_macro_name}_HASSIGNED${caps}"
-      echo "#define ${package_macro_name}_HASUNSIGNED${caps}"
-    fi >> $sysdeps/sysdeps.h
+  for t in size uid gid pid time dev ino; do
+    findtypesize "${t}_t" "$t"
+    findtypesign "${t}_t" "$t"
   done
-  rm -f output-types
   echo "  ... done"
 }
 
-- 
2.13.6

